package cn.ezios.huo.system.biz.login;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.ezios.huo.common.mvc.Ret;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/login")
@RequiredArgsConstructor
public class LoginController {
    private final LoginService loginService;

//    @RequestMapping("")
    public ResponseEntity<String> login() {
        StpUtil.login(10001,"chrome-x");
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String tokenValue = tokenInfo.getTokenValue();
        return ResponseEntity.ok(tokenValue);
    }

    @PostMapping("")
    public Ret login(@RequestBody LoginAuthData loginAuthData){
        // code唯一性校验

        // 验证码校验


        // 密码校验
        Ret ret  ;
        String username = loginAuthData.getUsername();
        // 根据username看下是用手机号还是用户名去验证
        if(username!=null&&username.startsWith("1")){
            ret = loginService.loginByPhone(loginAuthData);
        }else{
            ret = loginService.loginByLoginName(loginAuthData);
        }

        return ret;
    }

    @GetMapping("out")
    public Ret logout(){
        StpUtil.logout();
        return Ret.ok();
    }
}
